package vn.wallet.app.dao.impl;

// Generated Oct 23, 2012 10:05:44 PM by Hibernate Tools 3.4.0.CR1

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.internal.CriteriaImpl;
import org.hibernate.transform.ResultTransformer;
import org.primefaces.model.SortOrder;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import vn.wallet.app.dao.IBudgetDAO;
import vn.wallet.app.domain.Budget;

@Repository
public class BudgetDAO extends GenericDAO<Budget> implements IBudgetDAO {

	public BudgetDAO() {
		super(Budget.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Budget> getByName(String name) throws Exception {
		Criteria crit = getSessionFactory().getCurrentSession().createCriteria(Budget.class);
		crit.add(Restrictions.like("name", "%" + name + "%"));

		return crit.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getTransaction(Budget budget) throws Exception {
		return super.executeQuery("from Payment where budget_id = :budgetID", "budgetID", budget.getId());
	}

	// @Override
	// public Budget getById(int id) throws Exception {
	// return (Budget) getSessionFactory().getCurrentSession().get(Budget.class,
	// id);
	// }
	//
	// @Override
	// public void save(Budget obj) throws Exception {
	// //getSessionFactory().getCurrentSession().persist(obj);
	// //System.out.println(obj.getId().getId());
	// super.save(obj);
	// }
	//
	// @Override
	// public void update(Budget obj) throws Exception {
	// getSessionFactory().getCurrentSession().merge(obj);
	// System.out.println(obj.getId().getId());
	// }
	//
	// @Override
	// public void delete(Budget obj) throws Exception {
	// getSessionFactory().getCurrentSession().delete(obj);
	// }
	//
	// @Override
	// public void delete(int id) throws Exception {
	// getSessionFactory().getCurrentSession().delete(getById(id));
	// }
	//
	// @SuppressWarnings("unchecked")
	// @Override
	// public List<Budget> getAll() throws Exception {
	// return
	// getSessionFactory().getCurrentSession().createQuery("from Budget").list();
	// }
	//
	// @Override
	// public int getCount() throws Exception {
	// return (Integer)
	// getSessionFactory().getCurrentSession().createQuery("select count(*) from Budget").uniqueResult();
	// }

	@SuppressWarnings("unchecked")
	@Override
	public List<Budget> getAllByUserId(int userId) throws Exception {
		Criteria crit = getSessionFactory().getCurrentSession().createCriteria(Budget.class);
		crit.add(Restrictions.eq("userId", userId));
		return crit.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> searchUser(int idx, int pageSize, String sortField,
			SortOrder sortOrder, Budget budget, boolean ignoreStatus) throws Exception {

		List<Object> result = new ArrayList<Object>();
		List<Object> metaData = new ArrayList<Object>();

		// create criteria
		Criteria crit = getSessionFactory().getCurrentSession().createCriteria(Budget.class);

		// generate sample budget
		Example exampleBudget = Example.create(budget);
		exampleBudget.enableLike();
		exampleBudget.excludeZeroes();

		if (ignoreStatus) {
			exampleBudget.excludeProperty("status");
		}

		crit.add(exampleBudget);

		if (StringUtils.hasText(sortField)) {
			if (sortOrder.equals(SortOrder.ASCENDING)) {
				crit.addOrder(Order.asc(sortField));
			} else {
				crit.addOrder(Order.desc(sortField));
			}
		} else { // default
			crit.addOrder(Order.desc("id"));
		}

		// -----------------get meta data
		// get CriteriaImpl
		CriteriaImpl critImpl = (CriteriaImpl) crit;
		// save current projection & Result Transformer
		Projection orgProjection = critImpl.getProjection();
		ResultTransformer orgResultTransformer = critImpl.getResultTransformer();
		// get total row count
		long rowCount = ((Long) crit.setProjection(Projections.rowCount()).uniqueResult());

		// ----add meta data into return list
		metaData.add(rowCount);

		// set data back to original
		crit.setProjection(orgProjection);
		crit.setResultTransformer(orgResultTransformer);
		// ---------------------
		// get result
		crit.setFirstResult(idx);
		crit.setMaxResults(pageSize);
		result = crit.list();

		// add meta data into first idx
		result.add(0, metaData);

		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Budget> findByExample(Budget obj) throws Exception {
		List<Budget> lst = null;
		Example ex = Example.create(obj);
		ex.enableLike(MatchMode.ANYWHERE);
		ex.ignoreCase();

		lst = getSessionFactory().getCurrentSession().createCriteria(Budget.class)
				.add(ex)
				.list();

		return lst;
	}

	@Override
	public List<Budget> search() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
